package com.example.concurrentprogramming8016.reetrantlock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author liyanhao
 * @Date 2023-01-05 18:27
 */
public class ReentrantLockDemo {
    private static int sum = 0;
    private static ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                reentrantLock.lock();
                try {
                    for (int j = 0; j < 10000; j++) {
                        ++sum;
                    }
                } finally {
                    reentrantLock.unlock();
                }
            }, "thread-" + i).start();
        }
        TimeUnit.SECONDS.sleep(3);
        System.out.println(sum);
    }
}
